import { getToken, getBaseApiFrefix } from '@/libs/auth'
import { Toast } from 'vant'

Vue.use(Toast)
/*
 * get method
 */
function getAjax({
  method = 'get',
  url,
  data,
  headers = {
    'Content-Type': 'application/json;charset=utf-8',
    Authorization: 'Bearer ' + getToken(),
  },
  report = false,
}) {
  return new Promise((resolve, reject) => {
    api.ajax({ url, method, data, headers, report }, (ret, err) => {
      if (err) {
        reject(err)
      }
      if (ret.code == 401) {
        meassageDiag()
      }
      resolve(ret)
    })
  })
}

/*
 * delete method
 */
function deleteAjax({
  method = 'delete',
  url,
  data,
  headers = {
    'Content-Type': 'application/json;charset=utf-8',
    Authorization: 'Bearer ' + getToken(),
  },
  report = false,
}) {
  return new Promise((resolve, reject) => {
    api.ajax({ url, method, data, headers, report }, (ret, err) => {
      if (err) {
        reject(err)
      }
      resolve(ret)
    })
  })
}

function postFileAjax({
  method = 'post',
  url,
  data,
  headers = {
    Authorization: 'Bearer ' + getToken(),
  },
  report = false,
}) {
  return new Promise((resolve, reject) => {
    api.ajax({ url, method, data, headers, report }, (ret, err) => {
      if (err) {
        reject(err)
      }
      resolve(ret)
    })
  })
}

function postAjax({
  method = 'post',
  url,
  data,
  headers = {
    'Content-Type': 'application/json;charset=utf-8',
    Authorization: 'Bearer ' + getToken(),
  },
  report = false,
}) {
  return new Promise((resolve, reject) => {
    api.ajax({ url, method, data, headers, report }, (ret, err) => {
      if (err) {
        reject(err)
      }
      if (ret.code) {
        if (ret.code == 401) {
          meassageDiag()
          return
        }
      }

      resolve(ret)
    })
  })
}

//极光推送
export function bindAliasAndTags(userName) {
  // 极光推送
  var jpush = api.require('ajpush')
  var param = { alias: userName, sequence: 101 }
  if (api.systemType == 'ios') {
    jpush.setAlias(param, function (ret) {
      var statusCode = ret.statusCode
    })
  } else {
    jpush.bindAliasAndTags(param, function (ret) {
      var statusCode = ret.statusCode
    })
  }

  if (api.systemType != 'ios') {
    jpush.init(function (ret, err) {
      if (ret && ret.status) {
        api.addEventListener({ name: 'pause' }, function (ret, err) {
          onPause() //监听应用进入后台，通知jpush暂停事件
        })
        api.addEventListener({ name: 'resume' }, function (ret, err) {
          onResume() //监听应用恢复到前台，通知jpush恢复事件
        })
      } else {
        alert('连接消息服务器失败!请联系管理员')
      }
    })
  }
}

function meassageDiag() {
  Toast('登录状态已过期...')
  login()
}
function login() {
  api.openWin({
    name: 'login',
    url: './login-login.html',
    rect: {
      x: 0,
      y: 150,
      w: api.winWidth,
      h: api.winHeight,
    },
  })
}
function postWithoutTkn({
  method = 'post',
  url,
  data,
  headers = {
    'Content-Type': 'application/json;charset=utf-8',
  },
  report = false,
}) {
  return new Promise((resolve, reject) => {
    api.ajax({ url, method, data, headers, report }, (ret, err) => {
      if (err) {
        reject(err)
      }
      resolve(ret)
    })
  })
}

export const _httpClient = {
  async get(url) {
    return await getAjax({ url })
  },
  async delete(url) {
    return await deleteAjax({ url })
  },
  async postWithoutTkn(url, data) {
    return await postWithoutTkn({ url, data })
  },
  async post(url, data) {
    return await postAjax({ url, data })
  },
  async postFile(url, data) {
    return await postFileAjax({ url, data })
  },

  getUploadCycle() {
    return 10
  },

  getApiPrefix() {
    let prefix = getBaseApiFrefix()
    if (prefix != '' && prefix != null && prefix != undefined) {
      return prefix
    } else {
      // return 'http://10.211.55.5:5001'
      return 'http://10.13.105.11:5001'  
      // return 'http://10.13.6.133:5001'
    }
  },
}
